//
//  MainCollectionViewController.m
//  ShaLi
//
//  Created by dllo on 15-4-15.
//  Copyright (c) 2015年 HANJR. All rights reserved.
//

#import "MainCollectionViewController.h"
#import "MainCollectionViewTransitionLayout.h"
#import "HotCollectionViewCell.h"
#import "UIImageView+WebCache.h"
#import "MyConnection.h"
#import "FirstViewController.h"

#define MAX_COUNT 20
#define CELL_ID @"CELL_ID"
#define HOTURL @"http://www.duitang.com/napi/index/hot/"

@interface MainCollectionViewController ()

@end

@implementation MainCollectionViewController

- (void)dealloc
{
    [_collectionArray release];
    [super dealloc];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.collectionArray = [NSMutableArray array];
    }
    return self;
}

- (id)initWithCollectionViewLayout:(UICollectionViewLayout *)layout
{
    if (self = [super initWithCollectionViewLayout:layout])
    {
        [self.collectionView registerClass:[HotCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
        [self.collectionView setBackgroundColor:[UIColor clearColor]];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor clearColor];
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:@"sender%2Calbum%2Cicon_url&start=0" forKey:@"include_fields"];
    [dic setObject:@"24" forKey:@"limit"];
    __block MainCollectionViewController *my = self;
    [MyConnection connectWithUrl:HOTURL parmaters:dic finishBlock:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableDictionary *dic1 = [dic objectForKey:@"data"];
        self.collectionArray = [NSMutableArray array];
        for (NSDictionary *item in dic1[@"object_list"]) {
            [_collectionArray addObject:item];
        }
        [my.collectionView reloadData];
    }];
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    FirstViewController *push = [[FirstViewController alloc] init];
    [self.navigationController pushViewController: push animated:YES];
}


#pragma mark - Hide StatusBar
- (BOOL)prefersStatusBarHidden
{
    return YES;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HotCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    cell.layer.cornerRadius = 4;
    cell.clipsToBounds = YES;
    
    NSMutableDictionary *dic = [self.collectionArray objectAtIndex:indexPath.row];
    
    cell.label.text = [dic objectForKey:@"msg"];
    NSMutableDictionary *dic1 = [dic objectForKey:@"photo"];
    [cell.imageView sd_setImageWithURL:[dic1 objectForKey:@"path"]];
    
    
    return cell;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return MAX_COUNT;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (UICollectionViewTransitionLayout *)collectionView:(UICollectionView *)collectionView
                        transitionLayoutForOldLayout:(UICollectionViewLayout *)fromLayout newLayout:(UICollectionViewLayout *)toLayout
{
    MainCollectionViewTransitionLayout *transitionLayout = [[MainCollectionViewTransitionLayout alloc] initWithCurrentLayout:fromLayout nextLayout:toLayout];
    return transitionLayout;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    //     Adjust scrollView decelerationRate
    self.collectionView.decelerationRate = self.class != [MainCollectionViewController class] ? UIScrollViewDecelerationRateNormal : UIScrollViewDecelerationRateFast;
}

@end